package com.steven.im.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.makeramen.roundedimageview.RoundedImageView;
import com.squareup.picasso.Picasso;
import com.steven.im.App;
import com.steven.im.R;
import com.steven.im.entity.IMMsgList;
import com.steven.im.utils.IMTimeUtils;

import java.util.List;

public class IMChatMsgAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private List<IMMsgList> data;
    private Activity activity;

    public IMChatMsgAdapter(Activity activity, LayoutInflater inflater, List<IMMsgList> data) {
        this.activity = activity;
        this.inflater = inflater;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView( int i, View view, ViewGroup viewGroup) {
        final Holder holder;
        if (view == null) {
            view = inflater.inflate(R.layout.item_imchatmsg, null);
            holder = new Holder();
            holder.tv_imName = view.findViewById(R.id.tv_imName);
            holder.tv_imMsg = view.findViewById(R.id.tv_imMsg);
            holder.tv_imTime = view.findViewById(R.id.tv_imTime);
            holder.im_ivHead = view.findViewById(R.id.im_ivHead);
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }
        Picasso.with(App.getContext()).load(data.get(i).getImgUrl()).into(holder.im_ivHead);
        holder.tv_imName.setText(data.get(i).getName());
        holder.tv_imMsg.setText(data.get(i).getLastMsgContent());
        String tm=data.get(i).getTime();
        String time=IMTimeUtils.getInstance().getTime(tm);
        holder.tv_imTime.setText(time);
        //设置 显示内容
        return view;
    }

    private class Holder {
        TextView tv_imName, tv_imMsg, tv_imTime;
        RoundedImageView im_ivHead;
    }
}
